import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:my_app/base/foundation/flutter_extension.dart';

import '../foundation/jd_colors.dart';
import '../utils/string_tool.dart';
import 'image/jd_network_image.dart';

class CardContainer extends StatelessWidget {
  final double? width;
  final double? height;
  final Alignment? alignment;
  final BoxConstraints? constraints;

  final Widget child;
  final Color? color;
  final List<BoxShadow>? boxShadow;
  final Border? border;
  final BorderRadius? borderRadius;
  final EdgeInsets margin;
  final EdgeInsets padding;
  late final ImageProvider? imageProvider;

  CardContainer({
    Key? key,
    required this.child,
    this.width,
    this.height,
    this.alignment,
    this.constraints,
    this.boxShadow,
    this.color,
    this.border,
    this.borderRadius,
    this.margin = EdgeInsets.zero,
    this.padding = EdgeInsets.zero,
    String? backgroundUrl,
    String? backgroundOssUrl,
  }) : super(key: key) {
    if (StringTool.isNotEmpty(backgroundUrl)) {
      imageProvider = CachedNetworkImageProvider('$backgroundUrl');
    } else if (StringTool.isNotEmpty(backgroundOssUrl)) {
      imageProvider = CachedNetworkImageProvider('$kOssImg$backgroundOssUrl');
    } else {
      imageProvider = null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height,
      alignment: alignment,
      constraints: constraints,
      margin: margin,
      padding: padding,
      decoration: BoxDecoration(
        boxShadow: boxShadow,
        image: imageProvider != null
            ? DecorationImage(image: imageProvider!)
            : null,
        color: color ?? JDColors.cardColor(context),
        border: border,
        borderRadius: borderRadius ?? BorderRadius.circular(8.dp),
      ),
      child: child,
    );
  }
}
